package com.happyhome.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.happyhome.constants.AppConstant;
import com.happyhome.constants.ComUnitName;
import com.happyhome.constants.RecordStatus;
import com.happyhome.datamodel.Image;
import com.happyhome.datamodel.QHouse;
import com.happyhome.datamodel.QImage;
import com.happyhome.datamodel.QRoom;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class ImageDaoImpl extends GenericDAOImpl<Image, Long> implements ImageDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(ImageDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}

	@Override
	public List<Image> getAllImagesByHouseId(Long houseId) {
		JPQLQuery query = new JPAQuery(em());
		
		QImage image = QImage.image;
		QHouse house = QHouse.house;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(house.status.eq(RecordStatus.TRUE))
			 .and(house.idHouse.eq(houseId));
		
		query.from(image)
			.leftJoin(image.house, house)
			.where(where);
		
		List<Image> result = query.list(image);
		return result;
	}

	@Override
	public List<Image> getAllImagesByRoomId(Long roomId) {
		JPQLQuery query = new JPAQuery(em());
		
		QImage image = QImage.image;
		QRoom room = QRoom.room;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(room.status.eq(RecordStatus.TRUE))
			 .and(room.idRoom.eq(roomId));
		
		query.from(image)
			.leftJoin(image.room, room)
			.where(where);
		
		List<Image> result = query.list(image);
		return result;
	}

	@Override
	public List<Image> getAllImagesFree() {
		JPQLQuery query = new JPAQuery(em());
		
		QImage image = QImage.image;
//		QRoom room = QRoom.room;
//		QUser user = QUser.user;
//		QHouse house = QHouse.house;
//		QCustomer customer = QCustomer.customer;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(image.room.isNull())
			.and(image.user.isNull())
			.and(image.customer.isNull())
			.and(image.house.isNull());
		
		query.from(image)
			.where(where);
		
		List<Image> result = query.list(image);
		return result;
	}

	@Override
	public Image getImageByNewFileName(String fileName) {
		JPQLQuery query = new JPAQuery(em());
		
		QImage image = QImage.image;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(image.newFilename.eq(fileName));
		
		query.from(image)
			.where(where);
		
		Image result = query.uniqueResult(image);
		return result;
	}

	@Override
	public List<Image> getListImageByAId(Long aId, String type) {
		JPQLQuery query = new JPAQuery(em());
		
		QImage image = QImage.image;
		
		BooleanBuilder where = new BooleanBuilder();
		
		if(type.equals(AppConstant.HOUSES_CODE)){
			where.and(image.house.idHouse.eq(aId));
		}else if(type.equals(AppConstant.ROOMS_CODE)){
			where.and(image.room.idRoom.eq(aId));
		}
		
		query.from(image).where(where);
		
		List<Image> listResult = query.list(image);
		return listResult;
	}

	@Override
	public Image getImageByAIdIsDisplay(Long aId, String type) {
		JPQLQuery query = new JPAQuery(em());
		
		QImage image = QImage.image;
		
		BooleanBuilder where = new BooleanBuilder();
		
		if(type.equals(AppConstant.HOUSES_CODE)){
			where.and(image.house.idHouse.eq(aId)).and(image.isDisplay.eq(RecordStatus.TRUE));
		}else if(type.equals(AppConstant.ROOMS_CODE)){
			where.and(image.room.idRoom.eq(aId)).and(image.isDisplay.eq(RecordStatus.TRUE));
		}
		
		query.from(image).where(where);
		
		Image result = query.uniqueResult(image);
		return result;
	}

	@Override
	public Image getImageNewestAId(Long aId, String type) {
		Image result = null;
		
		JPQLQuery query = new JPAQuery(em());
		
		QImage image = QImage.image;
		
		BooleanBuilder where = new BooleanBuilder();
		
		if(type.equals(AppConstant.HOUSES_CODE)){
			where.and(image.house.idHouse.eq(aId));
		}else if(type.equals(AppConstant.ROOMS_CODE)){
			where.and(image.room.idRoom.eq(aId));
		}
		
		query.from(image).where(where).orderBy(image.dateCreated.desc());
		try{
			result = query.uniqueResult(image);
		}catch(NonUniqueResultException nonUniqueEx){
			List<Image> listImages = query.list(image);
			if(listImages != null && listImages.size() > 0){
				result = listImages.get(0);
			}
		}
		return result;
	}
}
